'use strict'
import React, { Component} from 'react';
import RootSiblings from 'react-native-root-siblings'
import BackToTopContainer from './BackToTopContainer.js';

let sibling = undefined

export default class BackToTop extends Component {

    static displayName = 'BackToTop';
    static propTypes = BackToTopContainer.propTypes;

    static show = (options) => {

        sibling = new RootSiblings(
            <BackToTopContainer
                {...options}
                visible={true}
            />
        );
        return sibling;
    };

    static hide = () => {
        if (sibling instanceof RootSiblings) {
            sibling.destroy();
        } else {
            console.warn(`BackToTop.hide expected a \`RootSiblings\` instance as argument.\nBut got \`${typeof sibling}\` instead.`);
        }
    };

    _backToTop = null;

    constructor(props) {
        super(props);
        this.state = {
            visible: this.props.visible,
        };
    };

    componentWillMount = () => {
        this._backToTop = new RootSiblings(
            <BackToTopContainer ref={(obj) =>{this.container = obj}}
                {...this.props}
                />
        );
    };

    componentWillReceiveProps = nextProps => {
        this._backToTop.update(
            <BackToTopContainer ref={(obj) =>{this.container = obj}}
                {...nextProps}
                />
        );
    };

    componentWillUnmount = () => {
        this._backToTop.destroy();
    };

    render(){
        return null;
    }

}

export {
    RootSiblings as Manager
};
